package com.woniuxy01;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 作者
 * @create 2025-11-18 14:04
 */
public class JdbcUtils {

    //创建连接对象
    public static Connection getConnection() throws Exception {
        String className="com.mysql.cj.jdbc.Driver";
        String url="jdbc:mysql:///db1";
        String user="root";
        String password="123";
        Class.forName(className);
        return DriverManager.getConnection(url, user, password);
    }

    //关闭资源
    public static void closeResource(Connection connection, Statement statement, ResultSet resultSet) throws SQLException {
        if(resultSet!=null){
            resultSet.close();
        }
        if(statement!=null){
            statement.close();
        }
        if(connection!=null){
            connection.close();
        }
    }

    //select * from stu
    //select * from stu where sid=?
    //select * from stu where age=? and sname=?
    //Object[]
    //List<Object>
    //执行增删改操作
    public static int executeUpdate(String sql,Object... objs) throws Exception {
        Connection connection = getConnection();
        PreparedStatement statement = getConnection().prepareStatement(sql);
        for(int i=0;i<objs.length;i++){
            statement.setObject(i+1, objs[i]);
        }
        int row = statement.executeUpdate();
        closeResource(connection, statement, null);
        return row;
    }


    public static <T> List<T> executeQueryList(Class<T> clazz, String sql, Object... objs) throws Exception {
        List<T> list=new ArrayList<>();
        Connection connection = getConnection();
        PreparedStatement statement = getConnection().prepareStatement(sql);
        for(int i=0;i<objs.length;i++){
            statement.setObject(i+1, objs[i]);
        }
        ResultSet resultSet = statement.executeQuery();
        ResultSetMetaData metaData = resultSet.getMetaData();
        while(resultSet.next()){
            //读取一条数据创建对象
            T t = clazz.newInstance();
            //匹配列名和属性名，给对象属性赋值
//            for(int i=0;i<metaData.getColumnCount();i++){
//                for(Field field : clazz.getDeclaredFields()){
//                    if(metaData.getColumnName(i+1).equals(field.getName())){
//                        field.setAccessible(true);
//                        field.set(t,resultSet.getObject(metaData.getColumnName(i+1)));
//                    }
//                }
//            }
            for(int i=0;i<metaData.getColumnCount();i++){
                String columnName = metaData.getColumnName(i + 1);
                Field field = clazz.getDeclaredField(columnName);
                field.setAccessible(true);
                field.set(t,resultSet.getObject(columnName));
            }


            list.add(t);
        }


        closeResource(connection, statement, null);
        return list;
    }
    public static <T> T executeQueryOne(Class<T> clazz, String sql, Object... objs) throws Exception {
        List<T> list = executeQueryList(clazz, sql, objs);
        if(list.size()==0){
            return null;
        }
        return list.get(0);
    }

    //执行查询操作
    //数据库的一张表，对应Java中的一个类，执行查询操作，可以直接返回对象(对象的属性值就是表中的列)
    //难点：表名不同(列名不同)---类名不同(属性不同)，使用反射解决
//    public static Stu executeQuery(String sql,Object... objs) throws Exception {
//        Connection connection = getConnection();
//        PreparedStatement statement = getConnection().prepareStatement(sql);
//        for(int i=0;i<objs.length;i++){
//            statement.setObject(i+1, objs[i]);
//        }
//        ResultSet resultSet = statement.executeQuery();
//        Stu stu=new Stu();
//        if(resultSet.next()){
//            stu.setSid(resultSet.getInt(1));
//            stu.setSname(resultSet.getString(2));
//            stu.setPwd(resultSet.getString(3));
//            stu.setAge(resultSet.getInt(4));
//        }
//        closeResource(connection, statement, null);
//        return stu;
//    }


}
